package com.eva.service.system.impl;

import com.eva.core.constants.Constants;
import com.eva.dao.system.SystemInterfaceConfigMapper;
import com.eva.dao.system.model.SystemInterfaceConfig;
import com.eva.service.common.CacheProxy;
import com.eva.service.system.SystemInterfaceConfigService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.regex.PatternSyntaxException;

/**
 * 系统接口配置Service实现
 * @author Eva.Caesar Liu
 * @date 2022/04/02 21:53
 */
@Slf4j
@Service
public class SystemInterfaceConfigServiceImpl implements SystemInterfaceConfigService {

    @Autowired
    private CacheProxy<String, List<SystemInterfaceConfig>> cacheProxy;

    @Autowired
    private SystemInterfaceConfigMapper systemInterfaceConfigMapper;

    @Override
    public SystemInterfaceConfig match(String uri) {
        List<SystemInterfaceConfig> configs = this.findEnableList();
        for (SystemInterfaceConfig config : configs) {
            try {
                if (uri.matches(config.getUriPattern())) {
                    return config;
                }
            } catch (PatternSyntaxException e) {
                log.error("EVA: handle interface config throw an exception, can not matches pattern '" + config.getUriPattern() + "'", e);
            }
        }
        return null;
    }

    @Override
    public Integer create(SystemInterfaceConfig systemInterfaceConfig) {
        systemInterfaceConfigMapper.insert(systemInterfaceConfig);
        // 清理缓存
        cacheProxy.remove(Constants.CacheKey.INTERFACE_CONFIG_LIST);
        return systemInterfaceConfig.getId();
    }

    @Override
    public void deleteById(Integer id) {
        SystemInterfaceConfig interfaceConfig = new SystemInterfaceConfig();
        interfaceConfig.setId(id);
        interfaceConfig.setDeleted(Boolean.TRUE);
        systemInterfaceConfigMapper.updateById(interfaceConfig);
        // 清理缓存
        cacheProxy.remove(Constants.CacheKey.INTERFACE_CONFIG_LIST);
    }

    @Override
    public void delete(SystemInterfaceConfig systemInterfaceConfig) {
        UpdateWrapper<SystemInterfaceConfig> deleteWrapper = new UpdateWrapper<>(systemInterfaceConfig);
        systemInterfaceConfigMapper.delete(deleteWrapper);
        // 清理缓存
        cacheProxy.remove(Constants.CacheKey.INTERFACE_CONFIG_LIST);
    }

    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        for (Integer id : ids) {
            this.deleteById(id);
        }
    }

    @Override
    public void updateById(SystemInterfaceConfig systemInterfaceConfig) {
        systemInterfaceConfigMapper.updateById(systemInterfaceConfig);
        // 清理缓存
        cacheProxy.remove(Constants.CacheKey.INTERFACE_CONFIG_LIST);
    }

    @Override
    public void updateByIdInBatch(List<SystemInterfaceConfig> systemInterfaceConfigs) {
        if (CollectionUtils.isEmpty(systemInterfaceConfigs)) {
            return;
        }
        for (SystemInterfaceConfig systemInterfaceConfig: systemInterfaceConfigs) {
            this.updateById(systemInterfaceConfig);
        }
    }

    @Override
    public SystemInterfaceConfig findById(Integer id) {
        return systemInterfaceConfigMapper.selectById(id);
    }

    @Override
    public SystemInterfaceConfig findOne(SystemInterfaceConfig systemInterfaceConfig) {
        QueryWrapper<SystemInterfaceConfig> wrapper = new QueryWrapper<>(systemInterfaceConfig);
        return systemInterfaceConfigMapper.selectOne(wrapper);
    }

    @Override
    public List<SystemInterfaceConfig> findList(SystemInterfaceConfig systemInterfaceConfig) {
        QueryWrapper<SystemInterfaceConfig> wrapper = new QueryWrapper<>(systemInterfaceConfig);
        return systemInterfaceConfigMapper.selectList(wrapper);
    }

    @Override
    public List<SystemInterfaceConfig> findEnableList() {
        // 先从缓存中读取
        List<SystemInterfaceConfig> configs = cacheProxy.get(Constants.CacheKey.INTERFACE_CONFIG_LIST);
        // 如果从缓存中没有读到，则从数据库中查询
        if (CollectionUtils.isEmpty(configs)) {
            QueryWrapper<SystemInterfaceConfig> wrapper = new QueryWrapper<>();
            wrapper.lambda()
                    .orderByDesc(SystemInterfaceConfig::getWeight)
                    .orderByDesc(SystemInterfaceConfig::getId)
                    .eq(SystemInterfaceConfig::getDeleted, Boolean.FALSE)
                    .eq(SystemInterfaceConfig::getDisabled, Boolean.FALSE);
            configs = systemInterfaceConfigMapper.selectList(wrapper);
        }
        // 写入缓存
        cacheProxy.put(Constants.CacheKey.INTERFACE_CONFIG_LIST, configs);
        return configs;
    }

    @Override
    public long count(SystemInterfaceConfig systemInterfaceConfig) {
        QueryWrapper<SystemInterfaceConfig> wrapper = new QueryWrapper<>(systemInterfaceConfig);
        return systemInterfaceConfigMapper.selectCount(wrapper);
    }
}
